package com.keyroy.game.puzzle_rpg.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.keyroy.game.puzzle_rpg.models.interfaces.EventListener;
import com.keyroy.game.puzzle_rpg.models.skill.SkillFolder;
import com.keyroy.game.puzzle_rpg.utils.FileUtil;
import com.keyroy.game.puzzle_rpg.views.IconView;

public class SkillFolderEditor extends Shell {
	private Text name_text;
	private Text icon_text;
	private Text info_text;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			SkillFolderEditor shell = new SkillFolderEditor(display, null);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public SkillFolderEditor(Display display, final EventListener<SkillFolder> listener) {
		super(display, SWT.DIALOG_TRIM);

		final IconView canvas = new IconView(this);
		canvas.setBounds(10, 10, 142, 148);

		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setBounds(158, 13, 35, 17);
		lblNewLabel.setText("\u540D\u79F0 : ");

		name_text = new Text(this, SWT.BORDER);
		name_text.setBounds(199, 10, 235, 23);

		Label lblNewLabel_1 = new Label(this, SWT.NONE);
		lblNewLabel_1.setBounds(158, 44, 35, 17);
		lblNewLabel_1.setText("\u56FE\u6807 : ");

		icon_text = new Text(this, SWT.BORDER | SWT.READ_ONLY);
		icon_text.setBounds(199, 41, 149, 23);

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String path = FileUtil.selectFile(getShell(), null, null, new String[] { "*.png", "*.gif" });
				if (path != null) {
					icon_text.setText(path);
				}
				canvas.setImage(path);
			}
		});
		btnNewButton.setBounds(354, 39, 80, 27);
		btnNewButton.setText("\u9009\u62E9\u6587\u4EF6");

		Label lblNewLabel_2 = new Label(this, SWT.NONE);
		lblNewLabel_2.setBounds(158, 73, 35, 17);
		lblNewLabel_2.setText("\u7B80\u4ECB : ");

		info_text = new Text(this, SWT.BORDER | SWT.WRAP);
		info_text.setBounds(199, 70, 235, 88);

		Button btnNewButton_1 = new Button(this, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (listener != null) {
					SkillFolder skillFolder = new SkillFolder();
					skillFolder.name = name_text.getText();
					skillFolder.icon = icon_text.getText();
					skillFolder.info = info_text.getText();
					listener.onEvent(skillFolder);
				}
				dispose();
			}
		});
		btnNewButton_1.setBounds(199, 164, 235, 27);
		btnNewButton_1.setText("\u5B8C\u6210");
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("\u6280\u80FD\u7EC4\u7F16\u8F91");
		setSize(450, 228);

	}

	@Override
	protected void checkSubclass() {
	}
}
